package com.ruoyi.web.controller.trans;

import java.util.List;

import com.ruoyi.trans.domain.BaSupplier;
import com.ruoyi.trans.service.IBaSupplierService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.trans.domain.BusTransportationDoc;
import com.ruoyi.trans.service.IBusTransportationDocService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 运输单据Controller
 * 
 * @author ruoyi
 * @date 2024-11-05
 */
@Controller
@RequestMapping("/trans/doc")
public class BusTransportationDocController extends BaseController
{
    private String prefix = "trans/doc";

    @Autowired
    private IBusTransportationDocService busTransportationDocService;
    @Autowired
    private IBaSupplierService baSupplierService;

    @RequiresPermissions("trans:doc:view")
    @GetMapping()
    public String doc()
    {
        return prefix + "/doc";
    }

    /**
     * 查询运输单据列表
     */
    @RequiresPermissions("trans:doc:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusTransportationDoc busTransportationDoc)
    {
        startPage();
        List<BusTransportationDoc> list = busTransportationDocService.selectBusTransportationDocList(busTransportationDoc);
        return getDataTable(list);
    }

    /**
     * 导出运输单据列表
     */
    @RequiresPermissions("trans:doc:export")
    @Log(title = "运输单据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusTransportationDoc busTransportationDoc)
    {
        List<BusTransportationDoc> list = busTransportationDocService.selectBusTransportationDocList(busTransportationDoc);
        ExcelUtil<BusTransportationDoc> util = new ExcelUtil<BusTransportationDoc>(BusTransportationDoc.class);
        return util.exportExcel(list, "运输单据数据");
    }

    /**
     * 新增运输单据
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("suppliers", baSupplierService.selectEnableSupplier());
        return prefix + "/add";
    }

    /**
     * 新增保存运输单据
     */
    @RequiresPermissions("trans:doc:add")
    @Log(title = "运输单据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusTransportationDoc busTransportationDoc)
    {
        if(busTransportationDocService.checkDocUni(busTransportationDoc)){
            return error("该时段预约已满");
        }
        return toAjax(busTransportationDocService.insertBusTransportationDoc(busTransportationDoc));
    }

    /**
     * 修改运输单据
     */
    @RequiresPermissions("trans:doc:edit")
    @GetMapping("/edit/{dId}")
    public String edit(@PathVariable("dId") Long dId, ModelMap mmap)
    {
        BusTransportationDoc busTransportationDoc = busTransportationDocService.selectBusTransportationDocByDId(dId);
        mmap.put("busTransportationDoc", busTransportationDoc);
        mmap.put("suppliers", baSupplierService.selectSupplierBysId(busTransportationDoc.getSupplier()));
        return prefix + "/edit";
    }

    /**
     * 修改保存运输单据
     */
    @RequiresPermissions("trans:doc:edit")
    @Log(title = "运输单据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusTransportationDoc busTransportationDoc)
    {
        if(busTransportationDocService.checkDocUni(busTransportationDoc)){
            return error("该时段预约已满");
        }
        return toAjax(busTransportationDocService.updateBusTransportationDoc(busTransportationDoc));
    }

    /**
     * 删除运输单据
     */
    @RequiresPermissions("trans:doc:remove")
    @Log(title = "运输单据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busTransportationDocService.deleteBusTransportationDocByDIds(ids));
    }
}
